package com.jwaoo.mobile.view.login.presenter;

import android.app.Activity;
import com.dx.as.common.CommonUtils;
import com.dx.as.common.log.LogUtil;
import com.dx.as.listener.IProInterface;
import com.dx.as.listener.IViewInterface;
import com.jwaoo.mobile.view.account.AccountBean;
import com.jwaoo.mobile.view.login.model.IModleInterface;
import com.jwaoo.mobile.view.login.model.LoginModel;
import java.util.Map;
import static http.HttpCode.ERROR_NOT_VERIFIED;

/**
 * Created by jwaoo on 2017/8/30.
 */

public class LoginPresenter extends IProInterface implements IModleInterface{
    private LoginModel model;
    private ILoginView loginView;
    private int login,accountInfo;
    public LoginPresenter(Activity activity, IViewInterface iViewInterface) {
        super(activity, iViewInterface);
        loginView = (ILoginView)iViewInterface;
        model = new LoginModel(getContext(),this);
    }

    @Override
    public void initialataData() {

    }

    @Override
    public int doLogin(Map map) {
        login = model.doLogin(map);
        return 0;
    }

    @Override
    public int getAcctountInfo() {
        accountInfo = model.getAcctountInfo();
        return 0;
    }

    @Override
    public int doActive() {
        return model.doActive();
    }

    private boolean isLocked = false;
    @Override
    public void onResponse(Object response, int flg) {
        if (response != null){
            if (flg == login){
                Map<String,Object> map = (Map<String,Object>)response;
                if (map.containsKey("token")){
                    String token = map.get("token").toString();
                    CommonUtils.saveToken(token);
                    int perfect = Integer.parseInt(map.get("perfect").toString());
                    CommonUtils.savePerfect(perfect);
                    doActive();
                    LogUtil.d("LoginActivity", CommonUtils.getPushToken());
                    int locked = Integer.parseInt(map.get("locked").toString());
                    isLocked= (locked == 1);
//                    //锁定
//                    if (locked == 1){
//                        loginView.onLocked();
//                    }else {
//                        //完善资料
//                        if (perfect == 0){
//                            loginView.onNotPerfect();
//                        }else {
//                            getAcctountInfo();
//                        }
//                    }
                    getAcctountInfo();
                }
            }else if (flg == accountInfo){
                AccountBean bean = (AccountBean) response;
                CommonUtils.saveAccount(bean.getLoginName());
                CommonUtils.saveIMAccount(bean.getId());
                CommonUtils.saveIMName(bean.getNickname());
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loginView.onGetAccountInfo(isLocked);
                    }
                });
            }
        }
    }

    @Override
    public void onFailure(int code, String errorMsg) {
        if (code == ERROR_NOT_VERIFIED) {
            loginView.onEmailNotverified(errorMsg);
        }else {
            super.onFailure(code,errorMsg);
        }
    }
}
